import tkinter as tk
from tkinter import messagebox


class TodoItem:
    def __init__(self, title, description, deadline, priority):
        self.title = title
        self.description = description
        self.deadline = deadline
        self.priority = priority
        self.completed = False

    def __str__(self):
        status = "完成" if self.completed else "未完成"
        return f"标题: {self.title}\n描述: {self.description}\n截止日期: {self.deadline}\n优先级: {self.priority}\n状态: {status}"


class TodoManagerApp:
    def __init__(self, root):
        self.root = root
        self.root.title("待办事项管理系统")

        self.todo_list = []
        self.current_index = -1

        self.setup_ui()

    def setup_ui(self):
        # 标签和输入框
        tk.Label(self.root, text="标题:").grid(row=0, column=0, padx=5, pady=5)
        self.title_entry = tk.Entry(self.root)
        self.title_entry.grid(row=0, column=1, padx=5, pady=5)

        tk.Label(self.root, text="描述:").grid(row=1, column=0, padx=5, pady=5)
        self.description_entry = tk.Entry(self.root)
        self.description_entry.grid(row=1, column=1, padx=5, pady=5)

        tk.Label(self.root, text="截止日期 (YYYY-MM-DD):").grid(row=2, column=0, padx=5, pady=5)
        self.deadline_entry = tk.Entry(self.root)
        self.deadline_entry.grid(row=2, column=1, padx=5, pady=5)

        tk.Label(self.root, text="优先级:").grid(row=3, column=0, padx=5, pady=5)
        self.priority_entry = tk.Entry(self.root)
        self.priority_entry.grid(row=3, column=1, padx=5, pady=5)

        # 按钮
        tk.Button(self.root, text="添加任务", command=self.add_task).grid(row=4, column=0, columnspan=2, padx=5,
                                                                          pady=10)
        tk.Button(self.root, text="查看所有任务", command=self.show_all_tasks).grid(row=5, column=0, columnspan=2,
                                                                                    padx=5, pady=10)
        tk.Button(self.root, text="编辑任务", command=self.edit_task).grid(row=6, column=0, columnspan=2, padx=5,
                                                                           pady=10)
        tk.Button(self.root, text="删除任务", command=self.delete_task).grid(row=7, column=0, columnspan=2, padx=5,
                                                                             pady=10)
        tk.Button(self.root, text="标记任务为完成", command=self.mark_completed).grid(row=8, column=0, columnspan=2,
                                                                                      padx=5, pady=10)
        tk.Button(self.root, text="查看已完成任务", command=self.show_completed_tasks).grid(row=9, column=0,
                                                                                            columnspan=2, padx=5,
                                                                                            pady=10)

        # 显示区域
        self.display_text = tk.Text(self.root, width=60, height=10)
        self.display_text.grid(row=10, column=0, columnspan=2, padx=5, pady=10)

    def add_task(self):
        title = self.title_entry.get()
        description = self.description_entry.get()
        deadline = self.deadline_entry.get()
        priority = self.priority_entry.get()

        if title and description and deadline and priority:
            item = TodoItem(title, description, deadline, priority)
            self.todo_list.append(item)
            messagebox.showinfo("成功", "任务添加成功!")
            self.clear_entries()
        else:
            messagebox.showerror("错误", "请填写所有字段!")

    def show_all_tasks(self):
        self.display_text.delete(1.0, tk.END)
        for index, item in enumerate(self.todo_list):
            self.display_text.insert(tk.END, f"任务编号: {index}\n")
            self.display_text.insert(tk.END, str(item) + "\n")
            self.display_text.insert(tk.END, "--------------------\n")

    def edit_task(self):
        index = self.get_current_index()
        if index != -1:
            title = self.title_entry.get()
            description = self.description_entry.get()
            deadline = self.deadline_entry.get()
            priority = self.priority_entry.get()
            if title and description and deadline and priority:
                self.todo_list[index].title = title
                self.todo_list[index].description = description
                self.todo_list[index].deadline = deadline
                self.todo_list[index].priority = priority
                messagebox.showinfo("成功", "任务编辑成功!")
                self.clear_entries()
                self.show_all_tasks()
            else:
                messagebox.showerror("错误", "请填写所有字段!")

    def delete_task(self):
        index = self.get_current_index()
        if index != -1:
            del self.todo_list[index]
            messagebox.showinfo("成功", "任务删除成功!")
            self.clear_entries()
            self.show_all_tasks()

    def mark_completed(self):
        index = self.get_current_index()
        if index != -1:
            self.todo_list[index].completed = True
            messagebox.showinfo("成功", "任务标记为完成!")
            self.show_all_tasks()

    def show_completed_tasks(self):
        self.display_text.delete(1.0, tk.END)
        completed_tasks = [task for task in self.todo_list if task.completed]
        for item in completed_tasks:
            self.display_text.insert(tk.END, str(item) + "\n")
            self.display_text.insert(tk.END, "--------------------\n")

    def get_current_index(self):
        try:
            index = int(self.title_entry.get())
            if 0 <= index < len(self.todo_list):
                self.current_index = index
                return index
            else:
                messagebox.showerror("错误", "无效的任务编号!")
                return -1
        except ValueError:
            messagebox.showerror("错误", "请输入有效的任务编号!")
            return -1

    def clear_entries(self):
        self.title_entry.delete(0, tk.END)
        self.description_entry.delete(0, tk.END)
        self.deadline_entry.delete(0, tk.END)
        self.priority_entry.delete(0, tk.END)


def main():
    root = tk.Tk()
    app = TodoManagerApp(root)
    root.mainloop()


if __name__ == "__main__":
    main()